package hu.diploma.android;

/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import hu.diploma.android.threads.ContactManagerService;
import hu.diploma.android.util.ServiceProviderImpl;
import hu.diploma.util.ServiceUtil;

import java.util.Date;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.text.format.DateFormat;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class ContactManagerClient extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		startService(new Intent(this, ContactManagerService.class));
		doBindService();
		ServiceUtil.setContactManagerService(mBoundService);
		Button button = (Button) findViewById(R.id.login_button);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				startActivityForResult(new Intent(ContactManagerClient.this, Login.class), 0);

			}
		});

		button = (Button) findViewById(R.id.sync_button);
		button.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

				mBoundService.synchronisation();

			}
		});

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.memu_settings:
			startActivityForResult(new Intent(ContactManagerClient.this, Settings.class), 0);
			return true;
		case R.id.menu_help:
			startActivity(new Intent(ContactManagerClient.this, Help.class));
			return true;
		case R.id.menu_exit:
			finish();
			return true;
		default:
			return super.onOptionsItemSelected(item);
		}

	}

	@Override
	protected void onResume() {
		super.onResume();
		TextView textView = (TextView) findViewById(R.id.last_sync_date);
		SharedPreferences settings = getSharedPreferences(ServiceProviderImpl.PREFS_NAME, MODE_WORLD_WRITEABLE);
		long lastSyncDate = settings.getLong("last_sync_date", new Date().getTime());
		textView.setText(DateFormat.getDateFormat(this).format(new Date(lastSyncDate)));

	}

	@Override
	protected void onPause() {

		super.onPause();
	}

	private boolean mIsBound;

	private ContactManagerService mBoundService;

	private final ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName className, IBinder service) {

			mBoundService = ((ContactManagerService.LocalBinder) service).getService();

			Toast.makeText(ContactManagerClient.this, "onServiceConnected", Toast.LENGTH_SHORT).show();
		}

		@Override
		public void onServiceDisconnected(ComponentName className) {

			mBoundService = null;
			Toast.makeText(ContactManagerClient.this, "onServiceDisconnected", Toast.LENGTH_SHORT).show();
		}
	};

	void doBindService() {

		bindService(new Intent(ContactManagerClient.this, ContactManagerService.class), mConnection,
				Context.BIND_AUTO_CREATE);
		mIsBound = true;
	}

	void doUnbindService() {
		if (mIsBound) {

			unbindService(mConnection);
			mIsBound = false;
		}
	}

	@Override
	protected void onDestroy() {
		super.onDestroy();
		doUnbindService();
	}

}
